package cn.kgc.secrity;


import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-03-06 9:18
 */
@Configuration   //配置类
@EnableWebSecurity  //启动secrity功能
@EnableGlobalMethodSecurity(prePostEnabled = true)//启用全局方法权限管理功能
public class MySecutityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权

        //开启表单登陆功能

        http.authorizeRequests()//对请求进行授权
                .antMatchers("/static/**", "/jsp/index.jsp")//使用ant风格设置需要授权的地址
                .permitAll()//允许上面使用ant风格设置的全部请求
                .anyRequest()//其他未设置的全部请求
                .authenticated()//需要认证
                .and()
                .csrf()
                .disable() // 禁用CSRF功能，如果不禁用，只能发post请求退出，否则报错
        ;

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //认证

        auth.inMemoryAuthentication()
                .passwordEncoder(new BCryptPasswordEncoder())
                .withUser("test")
                .password(new BCryptPasswordEncoder().encode("123456"))
                .roles("任海鹏");




//        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());

    }

//    @Bean
//    public BCryptPasswordEncoder bCryptPasswordEncoder() {
//        return new BCryptPasswordEncoder();
//    }
}
